package com.chixing.controller;


import com.chixing.annotation.AutoIdempotent;
import com.chixing.service.IRoomInventoryService;
import com.chixing.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author smith
 * @since 2025-09-30
 */
@CrossOrigin
@RestController
@RequestMapping("/room-inventory")
public class RoomInventoryController {
    @Autowired
    private IRoomInventoryService roomInventoryService;
    //查询房间库存
    @GetMapping("roomCount")
    public Result getRoomCount(@RequestParam Long hotelId,
                               @RequestParam  @DateTimeFormat(pattern = "yyyy-MM-dd") Date checkIn,
                               @RequestParam  @DateTimeFormat(pattern = "yyyy-MM-dd") Date checkOut){
        return roomInventoryService.getRoomCount(hotelId, checkIn, checkOut);
    }
    @PostMapping("decrCount")
    @AutoIdempotent // 为了防止一个订单多次扣减库存
    public Result decrCount(@RequestParam Long hotelId,
                            @RequestParam Long roomTypeId,
                            @RequestParam  @DateTimeFormat(pattern = "yyyy-MM-dd") Date checkIn,
                            @RequestParam  @DateTimeFormat(pattern = "yyyy-MM-dd") Date checkOut ){
        return roomInventoryService.decrCount(hotelId,checkIn,checkOut,roomTypeId);
    }
    @GetMapping("delay")
    public Result getRoomTypeRoomCount(@RequestParam Long hotelId,
                                       @RequestParam Long roomTypeId,
                                       @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate checkOut){
        return roomInventoryService.getRoomTypeRoomCount(hotelId, roomTypeId, checkOut);
    }
}
